import Vue from 'vue'
import App from './App.vue'
import router from './router'
import ElementUI from 'element-ui';     //引入element库
import 'element-ui/lib/theme-chalk/index.css'; 
import axios from 'axios'
Vue.prototype.$http=axios

//定制axios  具体可以定制的选项，参考https://www.kancloud.cn/yunye/axios/234845 请求配置一节
axios.defaults.baseURL="http://47.104.66.120:8081";
//添加拦截器，每次请求携带token
axios.interceptors.request.use(function (config) {
  const token = localStorage.getItem("token");
  config.headers.token=  token;
  return config;
});


Vue.config.productionTip = false
Vue.use(ElementUI);
new Vue({
  router,
  render: function (h) { return h(App) }
}).$mount('#app')
router.beforeEach((to,from,next)=>{
  let isLogined = localStorage.getItem("token");
  // console.log("是否登录"+isLogined);
  if(to.path=='/login'||to.path=="/logout"){
    // console.log("登陆请求，放行");
    next();
    return;
  }
  if(isLogined!=null){
    // console.log("已登录，放行");
    next();
    return;
  }
  Message({
    type:"error",
    message:"请您先登录"
  });
//this.$router 是在vue实例中使用
  //跳转
  router.push("/login");
});